/*
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 * Handler deals with incomming messages and displays send message with level
 * header through showMessage
 */

package WindowLogger;

/**
 *
 * @author xatrix
 */
import java.util.logging.ErrorManager;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

/**
 *
 * @author xatrix
 */
public class WindowLoggerHandler extends Handler {
    private static WindowLoggerHandler handler;
    private WindowLoggerDialog dialog = new WindowLoggerDialog();
            
    /** Creates a new instance of WindowLoggerHandler */
    private WindowLoggerHandler() {
        configure();
    }
    
    /**
     * Return singleton instance of handler
     * @return
     */
    public static synchronized WindowLoggerHandler getInstance() {
    		if (handler == null) {
			handler = new WindowLoggerHandler();
		}
		return handler;    
    }
    
    /**
     * Sets some basic configurations related with logging
     */
    private void configure() {
            LogManager manager = LogManager.getLogManager();
            String className = getClass().getName();
            String level = manager.getProperty(className + ".level");
            setLevel((level == null) ? Level.INFO : Level.parse(level));
//		String filter = manager.getProperty(className + ".filter");
//		String formatter =
//			manager.getProperty(className + ".formatter");
    }


    
    public void close() {
        

    }

    public void publish(LogRecord record) {
        String message = "";
        
        if (isLoggable(record)) {
                try {
                        message = record.getLevel().getName() + ":" +
                                record.getMessage() + "\n";    
                } catch (Exception e) {
                        reportError(null, e, ErrorManager.FORMAT_FAILURE);
                        return;
                }
                try {
                    dialog.showMessage(message);
                } catch (Exception e) {
                        reportError(null, e, ErrorManager.WRITE_FAILURE);
                }
        }
    }

    public void flush() {
    }
    
    /**
     * Function return WindowLoggerDialog
     * 
     * @return WindowLoggerDialog
     */
    public WindowLoggerDialog getWindowDialog() {
        return this.dialog;
    }
}
